//
//  ClubMyCollectView.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit

class ClubMyCollectCateView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    private var dataSource: [ClubGameCommunityFenLeiModel] = [
        ClubGameCommunityFenLeiModel(name: "游戏",type: "0",isSel: true),
        ClubGameCommunityFenLeiModel(name: "商品",type: "1",isSel: false),
        ClubGameCommunityFenLeiModel(name: "资讯",type: "3",isSel: false),
        ClubGameCommunityFenLeiModel(name: "帖子",type: "4",isSel: false),
    ]
    
    var updateCollectListView: ((_ type: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            collectionView
        }
        
        collectionView.fillContainer()
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubSquareDatailCateCell.self, forCellWithReuseIdentifier: ClubSquareDatailCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubSquareDatailCateCell.description(), for: indexPath) as? ClubSquareDatailCateCell else { return UICollectionViewCell() }
        cell.model = dataSource[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        var temp = [ClubGameCommunityFenLeiModel]()
        dataSource.forEach { model in
            var itemModel = model
            itemModel.isSel = false
            temp.append(itemModel)
        }
        temp[indexPath.row].isSel = true
        dataSource = temp
        updateCollectListView?(temp[indexPath.row].type)
        
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = dataSource[indexPath.row].name
        let font = 12.font_semiBold
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])

        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width + 24.0.Scale
        
        return CGSize(width: width, height: 24.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class ClubMyCollectCell: UITableViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(15.font_semiBold).line(2).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AFB1B3").font(11.font_medium).end()
    private let lookButton = UIIconButton(style: .leading(itemSpace: 2))
    private let commitButton = UIIconButton(style: .leading(itemSpace: 2))

    var model:ClubCollectGamesModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.loadImage(url: md.icon,placehold: "Common_picEmpty")
            titleLabel.text = md.name
            timeLabel.text = md.time
            if md.look == "0" {
                lookButton.setTitle("浏览", for: .normal)
            }else{
                lookButton.setTitle(md.look + "浏览", for: .normal)
            }
            if md.pinglun == "0" {
                commitButton.setTitle("评论", for: .normal)
            }else{
                commitButton.setTitle(md.pinglun + "评论", for: .normal)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            titleLabel
            timeLabel
            lookButton
            commitButton
        }
        
        iconImageV.leading(12.0.Scale).width(120.0.Scale).height(62.0.Scale).centerVertically()
        titleLabel.leading(144.0.Scale).trailing(12.0.Scale).top(8.0.Scale)
        timeLabel.leading(144.0.Scale)
        timeLabel.Bottom == iconImageV.Bottom
        commitButton.trailing(12.0.Scale).height(12.0.Scale)
        commitButton.Bottom == iconImageV.Bottom
        lookButton.height(12.0.Scale)
        lookButton.Bottom == iconImageV.Bottom
        lookButton.Trailing == commitButton.Leading - 9.0.Scale
        
        lookButton.style { v in
            v.iconSize = CGSize(width: 12.0.Scale, height: 12.0.Scale)
            v.setImage("Info_smallLook".toImage, for: .normal)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.titleLabel?.font = 12.font_medium
            v.isUserInteractionEnabled = false
            v.contentHorizontalAlignment = .right
        }
        
        commitButton.style { v in
            v.iconSize = CGSize(width: 12.0.Scale, height: 12.0.Scale)
            v.setImage("Info_smallCommit".toImage, for: .normal)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.titleLabel?.font = 12.font_medium
            v.isUserInteractionEnabled = false
            v.contentHorizontalAlignment = .right
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
